package com.xinxin.blockchain;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

public class MySqlGenerator {

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");

        // 代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig()
                .setOutputDir(projectPath + "/src/main/java").setFileOverride(true)
                .setAuthor("ys")
                .setServiceName("I%sDao").setServiceImplName("%sDao")
                .setMapperName("%sMapper").setActiveRecord(false)
                .setXmlName("%sMapper").setEnableCache(false).setBaseResultMap(false).setBaseColumnList(false);
        autoGenerator.setGlobalConfig(globalConfig);

        // 包配置
        PackageConfig packageConfig = new PackageConfig()
                .setParent("com.xinxin.blockchain")
                .setEntity("model")
                .setService("dao").setServiceImpl("dao.impl");
        autoGenerator.setPackageInfo(packageConfig);

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig()
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUrl("jdbc:mysql://52.81.8.176:3307/blockchain?characterEncoding=UTF-8&useSSL=false&noAccessToProcedureBodies=true&serverTimezone=GMT%2b8")
                .setUsername("db_yinge_btf_202002")
                .setPassword("7AVr7QrdS5rb");
        autoGenerator.setDataSource(dataSourceConfig);

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig()
                .setInclude("system_config")
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setControllerMappingHyphenStyle(true);
        autoGenerator.setStrategy(strategyConfig);

        // 自定义配置
        InjectionConfig injectionConfig = new InjectionConfig() {

            private FileOutConfig fileOutConfig = new FileOutConfig("/templates/mapper.xml.ftl") {

                @Override
                public String outputFile(TableInfo tableInfo) {
                    return projectPath + "/src/main/resources/mapper/"
                            + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                }
            };

            @Override
            public void initMap() {
                setFileOutConfigList(Collections.singletonList(fileOutConfig));
            }
        };
        autoGenerator.setCfg(injectionConfig);
        autoGenerator.setTemplate(new TemplateConfig().setXml(null));

        // 选择 freemarker 引擎需要指定如下加，注意 pom 依赖必须有！
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        autoGenerator.execute();
    }
}
